package com.firesrc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import jodd.db.DbQuery;
import jodd.db.DbThreadSession;
import jodd.db.oom.DbOomQuery;

import com.firesrc.bean.Keyword;
import com.firesrc.dao.KeywordDao;
import com.firesrc.dao.SuperDao;
import com.firesrc.util.SystemConfigConstant;

public class KeywordDaoImpl extends SuperDao<Keyword> implements KeywordDao {

  @Override
  public List<String> getTopicList() {
    DbThreadSession dbThreadSession = new DbThreadSession(cp);
    ResultSet rs = new DbQuery(dbThreadSession, "select distinct topic from record ").execute();
    LinkedList<String> linkedList = new LinkedList<String>();
    try {
      while (rs.next()) {
        linkedList.add(rs.getString(1));
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    dbThreadSession.closeSession();
    return linkedList;
  }

  @Override
  public void setMailSendByTopic(String topic) {
    DbThreadSession dbThreadSession = new DbThreadSession(cp);
    new DbQuery(dbThreadSession, "update  record  set mailstate='1' where topic='" + topic + "'").executeUpdateAndClose();
    dbThreadSession.closeSession();

  }

  @Override
  public List<Keyword> getTopSearchKeywordList() {
    DbThreadSession dbThreadSession = new DbThreadSession(cp);
    DbOomQuery q = new DbOomQuery("select top 1 * from keyword where state='0' and flag!=" + SystemConfigConstant.KEYWORD_STATE_FETCH);
    List<Keyword> findOne = q.list(Keyword.class);
    dbThreadSession.closeSession();
    return findOne;
  }

  @Override
  public void setSearchedByName(String keywordName) {
    new DbThreadSession(cp);
    new DbQuery("update keyword set state='1' where name='" + keywordName + "'").executeUpdateAndClose();
  }

  @Override
  public void setSearchedByNames(String keyWordNames) {
    new DbThreadSession(cp);
    new DbQuery("update keyword set state='1' where name in " + keyWordNames).executeUpdateAndClose();

  }

}
